<template>

    <div>
        <!-- Form -->
        <el-button type="primary" icon="el-icon-plus" @click="dialogFormVisible = true">添加</el-button>

        <el-dialog title="添加品牌" :visible.sync="dialogFormVisible" @close="clearFormData">
            <el-form :model="form">
                <el-form-item label="品牌名称" :label-width="formLabelWidth">
                    <el-input v-model="form.name" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="首字母" :label-width="formLabelWidth">
                    <el-input v-model="form.letter" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="活动名称" :label-width="formLabelWidth">
                    <el-upload
                            class="avatar-uploader"
                            action="http://api.blb.com/api/upload-api/upload/commons"
                            :show-file-list="false"
                            :on-success="handleAvatarSuccess"
                            :before-upload="beforeAvatarUpload">
                        <img v-if="form.image" :src="form.image" class="avatar">
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveOrUpdateBrand">确 定</el-button>
            </div>
        </el-dialog>
        <!--        表格-->
        <el-table
                :data="tableData"
                style="width: 100%">
            <el-table-column
                    prop="id"
                    label="编号"
            >
            </el-table-column>
            <el-table-column
                    prop="name"
                    label="名称"
            >
            </el-table-column>
            <el-table-column
                    label="图片">
                <template slot-scope="scope">
                    <el-image
                            style="width: 100px; height: 100px"
                            :fit="fit"
                            :src="scope.row.image"></el-image>
                </template>
            </el-table-column>
            <el-table-column
                    prop="letter"
                    label="首字母"
            >
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button
                            size="mini"
                            @click="handleEdit(scope.$index, scope.row)">编辑
                    </el-button>
                    <el-button
                            size="mini"
                            type="danger"
                            @click="handleDelete(scope.$index, scope.row)">删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <!--        分页-->
        <el-pagination
                background
                layout="prev, pager, next"
                :total="total"
                :current-page="current"
                @current-change="loadPage">
        </el-pagination>
    </div>
</template>

<script>
    export default {
        name: "Brand",
        data() {
            return {
                tableData: [],
                total: 0,
                current: 0,
                form: {id: "", name: "", image: "", letter: ""},
                dialogFormVisible: false,
                formLabelWidth: '120px'
            };
        },
        methods: {
            handleEdit(index, row) {
                // console.log(index, row);
                // 赋值
                this.form = row;
                this.dialogFormVisible=true;
            },
            // 删除品牌
            handleDelete(index, row) {
                this.$confirm('此操作将永久该书籍, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$http.delete("http://api.blb.com/api/goods-api/brand/" + row.id)
                        .then(res => {
                            if (res.data.code == 1) {
                                this.$message.success("删除成功")
                            }
                        });
                });
                this.loadPage(1);
            },
            loadPage(curr) {
                this.$http.get("http://api.blb.com/api/goods-api/brand/page/" + curr)
                    .then(res => {
                        if (res.data.code == 1) {
                            this.tableData = res.data.data.records;
                            this.total = res.data.data.total;
                            this.current = res.data.data.current;
                        }
                    });
            },
            saveOrUpdateBrand() {
                // 当id为空的时候添加
                if (this.form.id == "") {
                    this.$http.post("http://api.blb.com/api/goods-api/brand", this.form)
                        .then(res => {
                            if (res.data.code == 1) {
                                this.$message.success("恭喜你添加成功")
                            }
                        });
                }else {
                    // 不为空的时候更新数据
                    this.$http.put("http://api.blb.com/api/goods-api/brand", this.form)
                        .then(res => {
                            if (res.data.code == 1) {
                                this.$message.success("恭喜更你新品牌成功")
                            }
                        });
                }

                this.dialogFormVisible = false;
                this.loadPage(1);
            },
            // 图片上传
            handleAvatarSuccess(res, file) {
                // this.imageUrl = URL.createObjectURL(file.raw);
                // res是返回的UploadEntity对象
                this.form.image = res.url;
            },
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg';
                const isLt2M = file.size / 1024 / 1024 < 2;

                if (!isJPG) {
                    this.$message.error('上传头像图片只能是 JPG 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传头像图片大小不能超过 2MB!');
                }
                return isJPG && isLt2M;
            },
            // 清空数据
            clearFormData() {
                this.form = {id: "", name: "", image: "", letter: ""};
            }
        },
        mounted() {
            this.loadPage(1);
        }
    }
</script>

<style scoped>
    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }

    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }

    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
    }

    .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }
</style>